import { getLoginInfo } from '@/api/user'
const blackPathList = ['/orderdetail/index', '/viewOrdInfor/index']

export default {
  methods: {
    // 定时检查是否要授权登录
    setOtherLoginCheckTimer() {
      this.timer = setInterval(() => {
        if (this.confirming) {// 确认中
          return
        }
        getLoginInfo().then(res => {
          const data = res.data.data || {}
          if (data.count > 1 && data.empower == 1) {
            this.confirming = true
            this.$confirm('另一台设备尝试登录当前账号，允许其登录将退出当前账号，请确认是否授权其登录？', '提示', {
              confirmButtonText: '授权登录',
              cancelButtonText: '取消登录',
              type: 'warning',
              showClose: false,
              closeOnClickModal: false,
              closeOnPressEscape: false,
            }).then(() => {
              // 授权登录
              this.$store.dispatch("LogOut", {empower: 1}).then(() => {
                this.$router.push({
                  path: "/login"
                });
                this.clearTimer()
              });
            }).catch(() => {
              // 不授权
              this.$store.dispatch("LogOut", {unClearCache: true, empower: 2}).then(() => {});
            }).finally(() => {
              this.confirming = false
            });
          }
        })
      }, 10 * 1000)
    },
    clearTimer() {
      this.timer && clearInterval(this.timer)
      this.timer = null
    }
  },
  created() {
    const { path } = this.$route
    if (!blackPathList.includes(path)) {
      this.setOtherLoginCheckTimer()
    }
  },
  destroyed() {
    this.clearTimer()
  },
  render() {
    return null
  }
}